#include "skiprulesdelegate.h"
#include <QLineEdit>
#include <QComboBox>

SkipRulesDelegate::SkipRulesDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

void SkipRulesDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index){
    QStyledItemDelegate::paint(painter,option,index);
}

QWidget *SkipRulesDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    int c = index.column();
    if(c==0){
        QComboBox *comboBox = new QComboBox(parent);
        comboBox->setEditable(false);
        comboBox->addItem("No");
        comboBox->addItem("Yes");
        connect(comboBox,SIGNAL(activated(int)),this,SLOT(commitAndCloseEditor(int)));
        return comboBox;

    }
    else if(c==1){
        QComboBox *comboBox = new QComboBox(parent);
        comboBox->setEditable(false);
        comboBox->addItem("FixedString");
        comboBox->addItem("Wildcard");
        comboBox->addItem("WildcardUnix");
        comboBox->addItem("RegExp");
        comboBox->addItem("RegExp2");
        connect(comboBox,SIGNAL(activated(int)),this,SLOT(commitAndCloseEditor(int)));
        return comboBox;
    }
    else if(c==2){
        QLineEdit *lineEdit = new QLineEdit(parent);
        connect(lineEdit,SIGNAL(editingFinished()),this,SLOT(commitAndCloseEditor()));
        return lineEdit;
    }
    else {
        return QStyledItemDelegate::createEditor(parent,option,index);
    }
}

void SkipRulesDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    int c = index.column();
    if(c==0){
        QString yn = index.model()->data(index,Qt::DisplayRole).toString();
        QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
        comboBox->setEditText(yn);
    }
    else if(c==1){
        QString syntax = index.model()->data(index,Qt::DisplayRole).toString();
        QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
        comboBox->setEditText(syntax);
    }
    else if(c==2){
        QString pattern = index.model()->data(index,Qt::DisplayRole).toString();
        QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
        lineEdit->setText(pattern);
    }
    else{
        QStyledItemDelegate::setEditorData(editor,index);
    }

}

void SkipRulesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    int c = index.column();

    if((c==0)||(c==1)){
        QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
        model->setData(index,comboBox->currentText());
    }
    else if(c==2){
        QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
        model->setData(index,lineEdit->text());
    }
    else{
        QStyledItemDelegate::setModelData(editor,model,index);
    }
}

void SkipRulesDelegate::commitAndCloseEditor(){
    QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
    emit commitData(editor);
    emit closeEditor(editor);
}

void SkipRulesDelegate::commitAndCloseEditor(int){
    QComboBox *editor = qobject_cast<QComboBox *>(sender());
    emit commitData(editor);
    emit closeEditor(editor);
}
